package com.sunshine.lnuplus.ui.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.LayoutInflater
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.adapter.ScoreRecyclerAdapter
import com.sunshine.lnuplus.base.BaseActivity
import com.sunshine.lnuplus.callback.LoginCallBack
import com.sunshine.lnuplus.internet.LoadScore
import com.sunshine.lnuplus.model.ScoreData
import com.sunshine.lnuplus.utils.CryptoUtils
import com.sunshine.lnuplus.utils.StatusBarUtils
import com.sunshine.lnuplus.utils.TagHelper
import com.sunshine.lnuplus.view.DialogWithView
import com.sunshine.lnuplus.viewModel.LoginViewModel
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.activity_search_score.*

class SearchScoreActivity : BaseActivity() {

    private lateinit var viewModel: LoginViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search_score)

        viewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
        initView()
    }

    private fun initView() {
        StatusBarUtils.setStatusBarWithColor(this, color)
        title = "成绩查询"

        if(viewModel.getPassword().isNullOrBlank()){
            startActivityForResult(Intent(this, LoginActivity::class.java).putExtra("score", true), TagHelper.SEARCHSCORE)
        }else{
            val view = LayoutInflater.from(this).inflate(R.layout.dialog_loading, null, false)
            view.findViewById<TextView>(R.id.progress_text).text = "玩命加载中~"
            val loading = DialogWithView.newInstance(view, "正在登录",false, null, null)
            loading.show(supportFragmentManager, "loading")
            LoadScore(
                viewModel.getUser()!!,
                CryptoUtils.decrypt(viewModel.getPassword(), resources.getString(R.string.pw)),
                object : LoginCallBack {
                    override fun onSuccess() {
                        loading.dismiss()
                        handler.sendEmptyMessage(1)
                    }

                    override fun onFail(status: String) {
                        Looper.prepare()
                        loading.dismiss()
                        Toasty.warning(this@SearchScoreActivity, status, Toasty.LENGTH_SHORT).show()
                        Looper.loop()
                    }

                })
        }
    }

    @SuppressLint("HandlerLeak")
    val handler = object : Handler(){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (1 == msg.what){
                score_recycler.layoutManager = LinearLayoutManager(this@SearchScoreActivity)
                score_recycler.adapter = ScoreRecyclerAdapter(this@SearchScoreActivity)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == TagHelper.SEARCHSCORE){
            when(resultCode){
                TagHelper.LOGIN -> handler.sendEmptyMessage(1)
                Activity.RESULT_CANCELED -> finish()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if(ScoreData.scoreList != null){
            ScoreData.scoreList!!.clear()
        }
        score_recycler.removeAllViews()
    }
}
